<?php
    class AdminRoleBLL{
        //获取角色列表
        public function getRoleList(){
            $adminRoleDAL=new AdminRoleDAL();
            $result=$adminRoleDAL->getRoleList();

            return $result;
        }

        //获取部门列表
        public function getDepartmentList(){
            $adminRoleDAL=new AdminRoleDAL();
            $result=$adminRoleDAL->getDepartmentList();

            return $result;
        }

        //添加用户
        public function addUser($dataObject){
            $message=array();
            $adminRoleDAL=new AdminRoleDAL();
            $validateArray=$this->inputValidate($dataObject);

            if(count($validateArray)==0){
                $isUser=$this->isUser($dataObject->uid);
                if(!$isUser){
                    $dataObject->password=md5($dataObject->password);
                    $result=$adminRoleDAL->addUser($dataObject);
                    if($result){
                        $message=array(
                            'code'=>'2001',
                            'message'=>'添加成功',
                            'url'=>'./admin-role.php'
                        );
                    }else{
                        $message=array(
                            'code'=>'2004',
                            'message'=>'添加失败',
                            'url'=>'./admin-role.php'
                        );
                    }
                }else{
                    $message=array(
                        'code'=>'2002',
                        'message'=>'用户名已存在！',
                        'url'=>'./admin-role.php'
                    );
                }
            }else{
                $message=array(
                    'code'=>'2003',
                    'message'=>'输入不符合要求！',
                    'error'=>json_encode($validateArray,JSON_UNESCAPED_UNICODE),
                    'url'=>'./admin-role.php'
                );
            }
            
            return $message;
        }
        //判断用户是否存在
        public function isUser($uid){
            $flag=false;
            $adminRoleDAL=new AdminRoleDAL();
            $result=$adminRoleDAL->isUser($uid);
            if($result!=null){
                $flag=$result['count']==0?false:true;
            }

            return $flag;
        }
        //获取总页数
        public function getUserListPage($role,$pageSize=30,$search){
            $page=0;
            $adminRoleDAL=new AdminRoleDAL();
            $result=$adminRoleDAL->getUserCount($role,$search);

            $count=$result[0]['count'];
            if($count>$pageSize){
                if($count%$pageSize===0){
                    $page=(int)($count/$pageSize);
                }else{
                    $page=(int)($count/$pageSize)+1;
                }
            }

            return $page;
        }

        //获取用户列表
        public function getUserList($role,$page,$pageSize,$search){
            //$role=0,$page=0,$pageSize=0
            $adminRoleDAL=new AdminRoleDAL();
            $result=$adminRoleDAL->getUserList($role,$page*$pageSize,$pageSize,$search);
            
            return $result;
        }

        //获取用户所属部门
        public function getUserDepartment($uid){
            $adminRoleDAL=new AdminRoleDAL();
            $result=$adminRoleDAL->getUserDepartment($uid);

			$departmentName="";
			foreach($result as $department){
			    $departmentName=$departmentName.$department['department_name'].'、';
            }
            $departmentName=rtrim($departmentName,'、');

            return $departmentName;
        }

        //获取部门和角色列表
        public function getInitialList(){
            $publicDataBLL=new PublicDataBLL();

            $department_list=$publicDataBLL->getDepartmentList();
            $role_list=$publicDataBLL->getRoleList();

            $message=array(
                'department'=>$department_list,
                'role'=>$role_list
            );

            return $message;
        }

        //更新用户信息
        public function updateUserInformation($dataObject){
            $message=array();
            
            $validateArray=$this->inputValidate($dataObject);

            if(count($validateArray)>0){
                $message=array(
                    'code'=>'2003',
                    'message'=>'输入不符合要求！',
                    'error'=>json_encode($validateArray,JSON_UNESCAPED_UNICODE),
                    'url'=>'./admin-role.php'
                );
            }else{
                $adminRoleDAL=new AdminRoleDAL();
                $result=$adminRoleDAL->updateUserInformation($dataObject);
                if($result){
                    $message=array(
                        'code'=>'2001',
                        'message'=>'修改成功',
                        'url'=>'./admin-role.php'
                    );
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'修改失败',
                        'url'=>'./admin-role.php'
                    );
                }
            }
            
            return $message;
        }

        //删除用户 
        public function deleteUser($dataObject){
            $message=array();
            $userArray=$dataObject->users;
            $adminRoleDAL=new AdminRoleDAL();
            $result=$adminRoleDAL->deleteUser($userArray);
            if($result){
                $message=array(
                    'code'=>'2001',
                    'result'=>'',
                    'url'=>'./admin-role.php'
                );
            }else{
                $message=array(
                    'code'=>'2004',
                    'result'=>'',
                    'url'=>'./admin-role.php'
                );
            }
            return $message;
        }

        public function inputValidate($object){
            $error=array();
            $validate=new Validate();
            foreach($object as $key=>$value){
                if($key=="department"||$key=="departments"||$key=="role"){
                    continue;
                }else if(!$validate->regular($key,$value)){
                    $error[]=$key;
                }
            }
            return $error;
        }
    }
?>